import 'dart:io';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(new EventBusPlugin());
}

/*
 * 事件组件 <br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class EventBusPlugin extends StatefulWidget {
  State<StatefulWidget> createState() {
    return new EventBusPluginState();
  }
}

/*
 * 页面功能 <br/>
 */
class EventBusPluginState extends State<StatefulWidget> {
  EventBus mEventBus;
  dynamic value;

  void initState() {
    super.initState();
    initEventBus();
  }

  /*
   * 初始化EventBus对象
   */
  void initEventBus() {
    mEventBus = new EventBus();
    mEventBus.on().listen((event) {
      setState(() {
        value = event;
        print(value);
      });
    });
  }

  Widget build(BuildContext context) {
    AppBar appBar = AppBar(
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
        onPressed: () {},
      ),
      title: new Text('FlutterDesign'),
      centerTitle: true,
      actions: <Widget>[
        new Container(width: 0, height: 0),
        new Container(width: 0, height: 0),
      ],
      backgroundColor: Colors.blue,
    );

    return MaterialApp(
      home: new Scaffold(
        appBar: appBar,
        body: new Builder(builder: (BuildContext context) {
          return Padding(
            child: Card(
              child: Row(
                children: <Widget>[
                  RaisedButton(
                    child: Text('测试字符串发送'),
                    onPressed: () {
                      mEventBus.fire('12');
                    },
                  ),
                  RaisedButton(
                    child: Text('测试int发送'),
                    onPressed: () {
                      mEventBus.fire(1234343434);
                    },
                  ),
                  Text('${value}'),
                ],
              ),
            ),
            padding: EdgeInsets.all(10),
          );
        }),
        backgroundColor: Colors.white,
        bottomNavigationBar: null,
      ),
    );
  }
}
